import { DaliyTaskData, GroupTaskData } from "./GameDefine";
import { StorageKey } from "../common/Key";
import { GameState } from "./GameState";

export const TaskMgr = {
    init() {
        let data = G.data.getItem(StorageKey.DAILYTASK, { data: [], time: Date.now() });
        if (G.common.isToday(data.time)) {
            this._daliyData = data.data;
        } else {
            this._daliyData = [];
        }

        for (let i = 0; i < DaliyTaskData.length; i++) {
            if (!this._daliyData[i]) {
                if (DaliyTaskData[i].taskType == 3) {
                    let second = DaliyTaskData[i].taskCount * 60;
                    let coin = GameState.getProduction(second);
                    if (G.bignumber.compose(coin, 0) == 0) {
                        coin = 10000;
                    }
                    this._daliyData[i] = [coin, 0, 0];
                } else {
                    this._daliyData[i] = [DaliyTaskData[i].taskCount, 0, 0];
                }
            }
            G.data.setItem(StorageKey.DAILYTASK, { data: this._daliyData, time: Date.now() });
        }

        data = G.data.getItem(StorageKey.GROUPTASK, { data: []});
        this._groupData = data.data;
        for (let i = 0; i < GroupTaskData.length; i++) {
            if (!this._groupData[i]) {
                this._groupData[i] = [GroupTaskData[i].taskCount, 0, 0];
            }
            G.data.setItem(StorageKey.GROUPTASK, { data: this._groupData});
        }
    },

    getDaliyTask(index) {
        let data = this._daliyData[index];
        return data;
    },

    addDaliyTask(index, count, isBignumber = false) {
        let data = this._daliyData[index];
        if (data[2] == 1) {
            return;
        }
        if (isBignumber) {
            data[1] = G.bignumber.plus(data[1], count);
            if (G.bignumber.compose(data[1], data[0]) >= 0) {
                data[1] = data[0];
            }
        } else {
            data[1] += count;
            if (data[1] >= data[0]) {
                data[1] = data[0];
            }
        }

        G.data.setItem(StorageKey.DAILYTASK, { data: this._daliyData, time: Date.now() });
    },

    setDaliyTaskAchieve(index){
        let data = this._daliyData[index];
        if (data[2] == 1) {
            return;
        }
        data[2] = 1;
        G.data.setItem(StorageKey.DAILYTASK, { data: this._daliyData, time: Date.now() });
    },

    getGroupTask(index) {
        let data = this._groupData[index];
        return data;
    },

    addGroupTask(index, count, isBignumber = false) {
        let data = this._groupData[index];
        if (data[2] == 1) {
            return;
        }
        if (isBignumber) {
            data[1] = G.bignumber.plus(data[1], count);
            if (G.bignumber.compose(data[1], data[0]) >= 0) {
                data[1] = data[0];
            }
        } else {
            data[1] += count;
            if (data[1] >= data[0]) {
                data[1] = data[0];
            }
        }

        G.data.setItem(StorageKey.GROUPTASK, { data: this._groupData});
    },
    setGroupTaskAchieve(index){
        let data = this._groupData[index];
        if (data[2] == 1) {
            return;
        }
        data[2] = 1;
        G.data.setItem(StorageKey.GROUPTASK, { data: this._groupData});
    },
}